package com.baishancloud.log.foglog

import scala.beans.BeanProperty

/**
 *
 * @author ziqiang.wang 
 * @date 2022/2/15 10:42
 */
class Calculate extends Serializable

/**
 * 日志转化对象的key,
 */
case class LogKey(

                   timeAt5min: String,
                   timeAt1hour: String,
                   timeAt1day: String,
                   domain: Int,
                   httpCode: Short,
                   httpCodeCauseType: Short,
                   httpRequestType: Short,
                   country: Int,
                   province: Int,
                   isp: Int,
                   node: String,
                   cacheStatus: Short,
                   cacheLocation: Short,
                   serviceUnit: String,
                   serviceUnitIsp: Int,
                   serviceUnitProvince: Int,
                   serviceGroup: String,
                   serviceType: Short,
                   logType: Short,
                   sslProtocol: String,
                   isParent: Byte,
                   isIpv6: Byte,
                   isHttps: Byte,
                   isBigRequest: Byte
                 ) extends Serializable


/**
 * 日志转化对象的value
 */
case class LogValue(
                     sumRequestNum: Long,
                     sumTransBytes: Long,
                     sumTransTimeMS: Long,
                     sumNotRedirectBytes: Long,
                     sumNotRedirectNum: Long,
                     sumFirstPacketTimeMS: Long,
                     sumFirstFrameNum: Long,
                     sumFirstFrameTimeMS: Long,
                     sumSslHandshakeTimeMS: Long,
                     sumSslSessionReusedNum: Long,
                     sumTransSpeedKBps: Long,
                     sumSlow256KBpsNum: Long,
                     sumSlow512KBpsNum: Long,
                     sumElapsedWithin1SNum: Long,
                     sumElapsedWithin10SNum: Long
                   ) extends Serializable

/**
 * 日志转换对象以及计算对象
 *
 * @param domain                 域名Id
 * @param timeAt5min             五分钟粒度
 * @param timeAt1hour            一小时粒度
 * @param timeAt1day             一天粒度
 * @param httpCode               状态码 例:0 200 204 304 400
 * @param httpCodeCauseType      httpCode == 302:[1,2] httpCode == 403:[1,2,3] httpCode == 5xx:[1,2] httpCode == 2xx:[1]
 * @param httpRequestType        请求类型
 * @param country                国家Id
 * @param province               城市Id
 * @param isp                    运营商Id
 * @param node                   机房节点名称
 * @param cacheStatus            缓存状态 0:MISS 1:HIT -1:未知
 * @param cacheLocation          缓存位置 0:MEM 1:HDD 2:SSD
 * @param serviceUnit            存储为cache group 或 hostname
 * @param serviceUnitIsp         服务器机器所属的运营商id
 * @param serviceUnitProvince    服务器机器所属的省份id
 * @param serviceGroup           服务集群
 * @param serviceType            资源应用类型 0:常规CDN 1:类CDN 2:雾分发A类 3:雾分发B类
 * @param logType                日志种类 kernel:0 tengine:1  blb:2 squid:3 luban:4
 * @param sslProtocol            ssl协议 例:TLSv1.2 TLSv1.3......
 * @param isParent               0:边缘 1:父
 * @param isIpv6                 是否是ipv6
 * @param isHttps                是否是https
 * @param isBigRequest           是否是大请求 请求大小大于1M
 * @param sumRequestNum          总请求数
 * @param sumTransBytes          总流量
 * @param sumTransTimeMS         总响应时间
 * @param sumFirstPacketTimeMS   总首包时间
 * @param sumFirstFrameNum       总首帧请求数
 * @param sumFirstFrameTimeMS    总首帧时间
 * @param sumSslHandshakeTimeMS  总ssl握手耗时
 * @param sumSslSessionReusedNum 总ssl连接复用请求数
 * @param sumTransSpeedKBps      总下载速度，单位:KBps
 * @param sumSlow256KBpsNum      总下载速度低于256kbps的总请求数
 * @param sumSlow512KBpsNum      总下载速度低于512kbps的总请求数
 * @param sumElapsedWithin1SNum  耗时<=1s的日志量
 * @param sumElapsedWithin10SNum 耗时<=10s的日志量
 */
case class Log(
                @BeanProperty timeAt5min: String,
                @BeanProperty timeAt1hour: String,
                @BeanProperty timeAt1day: String,
                @BeanProperty domain: Int,
                @BeanProperty httpCode: Short,
                @BeanProperty httpCodeCauseType: Short,
                @BeanProperty httpRequestType: Short,
                @BeanProperty country: Int,
                @BeanProperty province: Int,
                @BeanProperty isp: Int,
                @BeanProperty node: String,
                @BeanProperty cacheStatus: Short,
                @BeanProperty cacheLocation: Short,
                @BeanProperty serviceUnit: String,
                @BeanProperty serviceUnitIsp: Int,
                @BeanProperty serviceUnitProvince: Int,
                @BeanProperty serviceGroup: String,
                @BeanProperty serviceType: Short,
                @BeanProperty logType: Short,
                @BeanProperty sslProtocol: String,
                @BeanProperty isParent: Byte,
                @BeanProperty isIpv6: Byte,
                @BeanProperty isHttps: Byte,
                @BeanProperty isBigRequest: Byte,

                @BeanProperty sumRequestNum: Long,
                @BeanProperty sumTransBytes: Long,
                @BeanProperty sumTransTimeMS: Long,
                @BeanProperty sumNotRedirectBytes: Long,
                @BeanProperty sumNotRedirectNum: Long,
                @BeanProperty sumFirstPacketTimeMS: Long,
                @BeanProperty sumFirstFrameNum: Long,
                @BeanProperty sumFirstFrameTimeMS: Long,
                @BeanProperty sumSslHandshakeTimeMS: Long,
                @BeanProperty sumSslSessionReusedNum: Long,
                @BeanProperty sumTransSpeedKBps: Long,
                @BeanProperty sumSlow256KBpsNum: Long,
                @BeanProperty sumSlow512KBpsNum: Long,
                @BeanProperty sumElapsedWithin1SNum: Long,
                @BeanProperty sumElapsedWithin10SNum: Long
              ) extends Serializable {

  def getKey: LogKey = {
    LogKey(timeAt5min, timeAt1hour, timeAt1day, domain, httpCode, httpCodeCauseType, httpRequestType, country, province, isp, node, cacheStatus, cacheLocation, serviceUnit, serviceUnitIsp, serviceUnitProvince, serviceGroup, serviceType, logType, sslProtocol, isParent, isIpv6, isHttps, isBigRequest)
  }

}


object Log extends Serializable {
  def apply(key: LogKey, value: LogValue): Log = {
    Log(key.timeAt5min, key.timeAt1hour, key.timeAt1day, key.domain, key.httpCode, key.httpCodeCauseType, key.httpRequestType, key.country, key.province, key.isp, key.node, key.cacheStatus, key.cacheLocation, key.serviceUnit, key.serviceUnitIsp, key.serviceUnitProvince, key.serviceGroup, key.serviceType, key.logType, key.sslProtocol, key.isParent, key.isIpv6, key.isHttps, key.isBigRequest, value.sumRequestNum, value.sumTransBytes, value.sumTransTimeMS, value.sumNotRedirectBytes, value.sumNotRedirectNum, value.sumFirstPacketTimeMS, value.sumFirstFrameNum, value.sumFirstFrameTimeMS, value.sumSslHandshakeTimeMS, value.sumSslSessionReusedNum, value.sumTransSpeedKBps, value.sumSlow256KBpsNum, value.sumSlow512KBpsNum, value.sumElapsedWithin1SNum, value.sumElapsedWithin10SNum)
  }
}